package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.core.domain.entity.SysUser;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.mapper.FamilyMemberMapper;
import com.zzyl.nursing.vo.member.CountByTimeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 预约信息Service业务层处理
 * 
 * @author alexis
 * @date 2024-11-13
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService
{
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询预约信息
     * 
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationById(Long id)
    {
        return getById(id);
    }

    /**
     * 新增预约信息
     *
     * @param reservation 预约信息
     */
    @Override
    public void addReservation(Reservation reservation) {
        // 设置默认状态
        if (ObjectUtils.isEmpty(reservation.getStatus())) {
            reservation.setStatus(0); // 默认状态为待报道
        }
        // 获取创建人Id
        reservation.setCreateBy(String.valueOf(UserThreadLocal.getUserId()));
        //插入数据
        try {
            save(reservation);
        } catch (Exception e) {
            throw new BaseException("不能重复预约");
        }
    }

    /**
     * 取消预约
     *
     * @param id 预约信息id
     */
    @Override
    public void cancelReservation(Long id) {
        // 拿到reservation并将状态设置为2
        Reservation reservation = selectReservationById(id);
        reservation.setStatus(2);
        // 设置更新人
        reservation.setUpdateBy(String.valueOf(UserThreadLocal.getUserId()));
        // 更新数据
        updateById(reservation);
    }

    /**
     * 获取已取消预约数量
     *
     * @return 已取消预约数量
     */
    @Override
    public Long getCancelledReservationCount() {
        // 获取当天开始时间和结束时间
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        // 获取创建人
        Long userId = UserThreadLocal.getUserId();
        Long count = reservationMapper.selectCount(Wrappers.<Reservation>lambdaQuery()
                .eq(Reservation::getCreateBy, userId)
                .eq(Reservation::getStatus, 2)
                .between(Reservation::getTime, startTime, endTime));
        // 获取当天取消预约的数量
        return count;
    }

    /**
     * 获取预约列表
     *
     * @param status 预约状态
     * @return 预约列表
     */
    @Override
    public List<Reservation> getReservationList(Integer status) {
        // 根据状态查询预约记录
        if (status == null) {
            // 如果状态为空，则查询所有预约记录
            return reservationMapper.selectList(Wrappers.<Reservation>lambdaQuery());
        } else {
            // 如果状态不为空，则查询特定状态的预约记录
            return reservationMapper.selectList(Wrappers.<Reservation>lambdaQuery().eq(Reservation::getStatus, status));
        }
    }

    /**
     * 获取预约数量
     *
     * @param time
     * @return
     */
    @Override
    public List<CountByTimeVo> getCountByTime(Long time) {
        // 获取当天开始时间和结束时间
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        // 查询预约数量
        List<CountByTimeVo> list = reservationMapper.getCountByTime(startTime,endTime);
        return list;
    }

    /**
     * 定时更新预约状态
     */
    @Override
    public void updateReservationStatus() {
        List<Reservation> reservations = reservationMapper.selectList(Wrappers.<Reservation>lambdaQuery()
                .lt(Reservation::getTime, LocalDateTime.now())
                .eq(Reservation::getStatus, 0));
        // 设置状态为已过期
        reservations.forEach(item -> item.setStatus(3));
        // 批量更新
        updateBatchById(reservations);
    }
}
